package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.*;
import com.ytjj.qmyx.admin.mapper.ProductSkuKeyMapper;
import com.ytjj.qmyx.admin.mapper.ProductSkuValueMapper;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.ProductSkuKey;
import com.ytjj.qmyx.admin.model.ProductSkuValue;
import com.ytjj.qmyx.admin.model.request.ProductSelectRequest;
import com.ytjj.qmyx.admin.model.request.SelectRequest;
import com.ytjj.qmyx.admin.model.request.SelectSpecRequest;
import com.ytjj.qmyx.admin.model.response.SelectResponse;
import com.ytjj.qmyx.admin.service.MemberService;
import com.ytjj.qmyx.admin.service.ProductSelectService;
import com.ytjj.qmyx.mall.mapper.*;
import com.ytjj.qmyx.mall.model.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProductSelectServiceImpl implements ProductSelectService {
    @Resource
    private ProductSelectMapper productSelectMapper;
    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private BankerMapper bankerMapper;
    @Resource
    private ProductSelectDao productSelectDao;
    @Resource
    private MemberService memberService;
    @Resource
    private ProductSelectSpecMapper productSelectSpecMapper;
    @Resource
    private ProductSelectSpecDao productSelectSpecDao;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductSelectSkuKeyMapper productSelectSkuKeyMapper;
    @Resource
    private ProductSelectSkuValueMapper productSelectSkuValueMapper;
    @Resource
    private ProductSkuKeyDao productSkuKeyDao;
    @Resource
    private ProductSkuKeyMapper productSkuKeyMapper;
    @Resource
    private ProductSkuValueMapper productSkuValueMapper;
    @Resource
    private ProductSkuValueDao productSkuValueDao;
    @Resource
    private ProductSpecMapper productSpecMapper;
    @Resource
    private ProductSpecDao productSpecDao;
    @Resource
    private ProductSelectImgMapper productSelectImgMapper;
    @Resource
    private ProductImgDao productImgDao;
    @Resource
    private ProductImgMapper productImgMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateReceStatus(ProductSelectRequest productSelectRequest) {
        Admin admin = memberService.getAdmin();
        //修改接单状态
        ProductSelect productSelect1 = new ProductSelect();
        productSelect1.setId(productSelectRequest.getId());
        productSelect1.setCompleteStatus((byte) 0);
        productSelect1.setReceStatus(productSelectRequest.getReceStatus());
        int count = productSelectMapper.updateByPrimaryKeySelective(productSelect1);
        if (count > 0) {
            ProductSelect productSelect = productSelectMapper.selectByPrimaryKey(productSelectRequest.getId());
            if (productSelect.getReceStatus() == 2) {
                productSelect.setUserId(admin.getId());
                productSelect.setUserName(admin.getName());
                productSelect.setCompleteStatus((byte) 0);
                productSelect.setTaskOpenTime(new Date());
            }
            if (productSelect.getReceStatus() == 3) {
                productSelect.setCompleteStatus((byte) 1);
                productSelect.setTaskCloseTime(new Date());
            }
            count = productSelectMapper.updateByPrimaryKey(productSelect);
        }
        return count;
    }

    @Override
    public CommonPage<SelectResponse> selectSelectList(SelectRequest selectRequest) {

        PageHelper.startPage(selectRequest.getPageNum(), selectRequest.getPageSize());
        List<SelectResponse> page = productSelectDao.selectSelectList(selectRequest);
        if (!CollectionUtils.isEmpty(page)) {
            for (SelectResponse selectResponse : page) {
                //根据categoryId拿到的是二级分类信息
                ProductCategory yxProductCategory = productCategoryMapper.selectByPrimaryKey(selectResponse.getCategoryId());
                selectResponse.setCategoryTitle(yxProductCategory.getTitle());
                //获取一级分类信息
                if (yxProductCategory.getPid() != 0) {
                    ProductCategory pcTop = productCategoryMapper.selectByPrimaryKey(yxProductCategory.getPid());
                    selectResponse.setCategoryTopTitle(pcTop.getTitle());
                } else if (yxProductCategory.getPid() == 0) {
                    selectResponse.setCategoryTopTitle("二级分类已为最上级分类");
                }
                //获取bankerName
                Banker banker = bankerMapper.selectByPrimaryKey(selectResponse.getBankerId());
                if (banker != null) {
                    selectResponse.setBankerName(banker.getName());
                }
            }
        }
        return CommonPage.restPage(page);
    }

    @Override
    public CommonPage<ProductSelectSpec> selectSpecList(SelectSpecRequest selectSpecRequest) {
        PageHelper.startPage(selectSpecRequest.getPageNum(), selectSpecRequest.getPageSize());
        List<ProductSelectSpec> productSelectSpecs = productSelectSpecDao.selectSpecList(selectSpecRequest);
        return CommonPage.restPage(productSelectSpecs);
    }


    @Override
    public int updateSpec(ProductSelectSpec productSelectSpec) {
        productSelectSpec.setUpdateTime(new Date());
        return productSelectSpecDao.updateSpec(productSelectSpec);
    }

    @Override
    public int completeSelectProductInfo(ProductSelectRequest productSelectRequest) {
        ProductSelect productSelect = productSelectMapper.selectByPrimaryKey(productSelectRequest.getId());
        if (productSelect == null) {
            throw new ApiException("商品不存在/已经删除");
        }
        productSelect.setSceneImg(productSelectRequest.getSceneImg());
        productSelect.setShrinkImg(productSelectRequest.getShrinkImg());
        productSelect.setPrice(productSelectRequest.getPrice());
        productSelect.setDescr(productSelectRequest.getDescr());
        productSelect.setSort(productSelectRequest.getSort());
        productSelect.setOriginalPrice(productSelectRequest.getOriginalPrice());
        productSelect.setCurPrice(productSelectRequest.getCurPrice());
        productSelect.setCostPrice(productSelectRequest.getCostPrice());
        productSelect.setId(productSelectRequest.getId());
        productSelect.setStock(productSelectRequest.getStock());
        productSelect.setGoodRatio(productSelectRequest.getGoodRatio());
        productSelect.setVolumeStr(productSelectRequest.getVolumeStr());
        productSelect.setUpdateTime(new Date());
        productSelect.setRate(productSelectRequest.getRate());
        productSelect.setRecommendDoc(productSelectRequest.getRecommendDoc());
        productSelect.setRecImgFlag(productSelectRequest.getRecImgFlag());
        productSelect.setRecDescr(productSelectRequest.getRecDescr());
        return productSelectMapper.updateByPrimaryKeySelective(productSelect);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSelectStatus(ProductSelectRequest productSelectRequest) {
        ProductSelect productSelect = new ProductSelect();
        productSelect.setId(productSelectRequest.getId());
        productSelect.setSelectStatus(productSelectRequest.getSelectStatus());
        int count = productSelectMapper.updateByPrimaryKeySelective(productSelect);
        if (count > 0) {
            ProductSelect productSelect1 = productSelectMapper.selectByPrimaryKey(productSelectRequest.getId());
            if (productSelect1 != null) {
                Product product = new Product();
                product.setProductNo(productSelect1.getProductNo());
                product.setProductName(productSelect1.getProductName());
                product.setProductImg(productSelect1.getProductImg());
                product.setSceneImg(productSelect1.getSceneImg());
                product.setShrinkImg(productSelect1.getShrinkImg());
                product.setPrice(productSelect1.getPrice());
                product.setDescr(productSelect1.getDescr());
                product.setSort(productSelect1.getSort());
                product.setOriginalPrice(productSelect1.getOriginalPrice());
                product.setStatus((byte) 2);   // 下架
                product.setAuditStatus(0);
                product.setCurPrice(productSelect1.getCurPrice());
                product.setCostPrice(productSelect1.getCostPrice());
                product.setStock(productSelect1.getStock());
                product.setWarehouse(productSelect1.getWarehouse());
                product.setWarehouseName(productSelect1.getWarehouseName());
                product.setWarehouseRule(productSelect1.getWarehouseRule());
                product.setCategoryId(productSelect1.getCategoryId());
                product.setVolumeStr(productSelect1.getVolumeStr());
                product.setAttributeJson(productSelect1.getAttributeJson());
                product.setCreateTime(new Date());
                product.setBankerId(productSelect1.getBankerId());
                product.setRate(productSelect1.getRate());
                product.setHomeFlag(0);
                product.setRecommendDoc(productSelect1.getRecommendDoc());
                product.setRecImgFlag(0);
                product.setRecDescr(productSelect1.getRecDescr());
                product.setDataType(productSelectRequest.getDataType());
                productMapper.insertSelective(product);
                //同步yx_product_select_sku_key到yx_product_sku_key
                ProductSelectSkuKeyExample productSelectSkuKeyExample = new ProductSelectSkuKeyExample();
                productSelectSkuKeyExample.createCriteria().andPidEqualTo(productSelect1.getProductLibraryId());
                List<ProductSelectSkuKey> productSelectSkuKeys = productSelectSkuKeyMapper.selectByExample(productSelectSkuKeyExample);
                for (ProductSelectSkuKey productSelectSkuKey : productSelectSkuKeys) {
                    ProductSkuKey productSkuKey = new ProductSkuKey();
                    productSkuKey.setPid(product.getId());
                    productSkuKey.setKid(productSelectSkuKey.getAttributeKeyId());
                    productSkuKey.setName(productSelectSkuKey.getName());
                    productSkuKey.setSort(productSelectSkuKey.getSort());
                    productSkuKey.setCreateTime(new Date());
                    ProductSkuKey productSkuKey1 = productSkuKeyDao.selectByPidAndKid(product.getId(), productSelectSkuKey.getAttributeKeyId());
                    if (productSkuKey1 == null) {
                        productSkuKeyMapper.insert(productSkuKey);
                    }
                }
                //同步yx_product_select_sku_value到yx_product_sku_value
                ProductSelectSkuValueExample productSelectSkuValueExample = new ProductSelectSkuValueExample();
                productSelectSkuValueExample.createCriteria().andPidEqualTo(productSelect1.getProductLibraryId());
                List<ProductSelectSkuValue> productSelectSkuValues = productSelectSkuValueMapper.selectByExample(productSelectSkuValueExample);
                for (ProductSelectSkuValue productSelectSkuValue : productSelectSkuValues) {
                    ProductSkuValue productSkuValue = new ProductSkuValue();
                    productSkuValue.setPid(product.getId());
                    productSkuValue.setVid(productSelectSkuValue.getAttributeValueId());
                    productSkuValue.setValue(productSelectSkuValue.getValue());
                    productSkuValue.setSort(productSelectSkuValue.getSort());
                    productSkuValue.setStatus(productSelectSkuValue.getStatus());
                    productSkuValue.setCreateTime(new Date());
                    ProductSkuValue productSkuValue1 = productSkuValueDao.selectByPidAndVid(product.getId(), productSelectSkuValue.getAttributeValueId());
                    if (productSkuValue1 == null) {
                        productSkuValueMapper.insert(productSkuValue);
                    }
                }
                //同步yx_product_select_spec到yx_product_spec
                ProductSelectSpecExample productSelectSpecExample = new ProductSelectSpecExample();
                productSelectSpecExample.createCriteria().andProductLibraryIdEqualTo(productSelect1.getProductLibraryId());
                List<ProductSelectSpec> productSelectSpecs = productSelectSpecMapper.selectByExample(productSelectSpecExample);
                for (ProductSelectSpec productSelectSpec : productSelectSpecs) {
                    ProductSpec productSpec = new ProductSpec();
                    productSpec.setProductId(product.getId());
                    productSpec.setSkuSpec(productSelectSpec.getSkuSpec());
                    productSpec.setPrice(productSelectSpec.getPrice());
                    productSpec.setProImg(productSelectSpec.getProImg());
                    productSpec.setBuySum(productSelectSpec.getBuySum());
                    productSpec.setCreateTime(new Date());
                    productSpec.setStatus(productSelectSpec.getStatus());
                    productSpec.setStock(productSelectSpec.getStock());
                    ProductSpec productSpec1 = productSpecDao.selectByProductIdAndSkuSpec(product.getId(), productSelectSpec.getSkuSpec());
                    if (productSpec1 == null) {
                        productSpecMapper.insert(productSpec);
                    }
                }
                //同步图库到yx_product_img
                ProductSelectImgExample productSelectImgExample = new ProductSelectImgExample();
                productSelectImgExample.createCriteria()
                        .andProductLibraryIdEqualTo(productSelect1.getId())
                        .andStatusEqualTo(1);
                List<ProductSelectImg> productSelectImgs = productSelectImgMapper.selectByExample(productSelectImgExample);
                for (ProductSelectImg productSelectImg : productSelectImgs) {
                    ProductImg productImg = new ProductImg();
                    productImg.setCreateTime(new Date());
                    productImg.setFileType(productSelectImg.getFileType());
                    productImg.setImgUrl(productSelectImg.getImg());
                    productImg.setProductId(product.getId());
                    productImg.setSort(productSelectImg.getSort());
                    productImg.setType(productSelectImg.getType());
                    ProductImg productImg1 = productImgDao.selectByProductIdAndImgUrl(product.getId(), productSelectImg.getImg());
                    if (productImg1 == null){
                        productImgMapper.insertSelective(productImg);
                    }
                }
            }
        }
        return count;
    }
}
